package com.lnking.lnkingplace.controller;


import com.lnking.common.http.ReturnResult;
import com.lnking.lnkingplace.Util.UserUtil;
import com.lnking.lnkingplace.mapper.IsolationPlaceMapper;
import com.lnking.lnkingplace.model.dto.placeProblem.PlaceProblemDto;
import com.lnking.lnkingplace.model.dto.placeProblem.PlaceProblemExcel;
import com.lnking.lnkingplace.model.enums.UserType;
import com.lnking.lnkingplace.model.request.placeProblem.PlaceProblemAddRequest;
import com.lnking.lnkingplace.model.request.placeProblem.PlaceProblemSearchRequest;
import com.lnking.lnkingplace.model.request.placeProblem.PlaceProblemUpdateRequest;
import com.lnking.lnkingplace.service.IPlaceProblemService;
import com.lnking.starter.export.utils.ExportUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangqt
 * @since 2022-09-03
 */
@RestController
@RequestMapping("/place-problem")
@Api(tags = "场所问题整改")
public class PlaceProblemController {

    @Resource
    UserUtil userUtil;
    @Resource
    IPlaceProblemService placeProblemService;
    @Resource
    IsolationPlaceMapper isolationPlaceMapper;

    @ApiOperation(value = "导出")
    @GetMapping("export")
    public ReturnResult export(PlaceProblemSearchRequest params, HttpServletResponse response) throws Exception {
        List<PlaceProblemDto> data = (List<PlaceProblemDto>)placeProblemService.pageList(params);
        ExportUtils.export(data, response, PlaceProblemExcel.class);
        return ReturnResult.success();
    }

    @PostMapping("import")
    @ApiOperation("导入")
    public ReturnResult importExcel(@RequestParam("file") MultipartFile file) throws Exception{
        placeProblemService.importExcel(file);
        return ReturnResult.success();
    }

    @PostMapping("add")
    @ApiOperation("新增")
    public ReturnResult add(@RequestBody PlaceProblemAddRequest params) throws Exception{
        placeProblemService.add(params);
        return ReturnResult.success();
    }

    @PostMapping("update")
    @ApiOperation("修改")
    public ReturnResult update(@RequestBody PlaceProblemUpdateRequest params) throws Exception{
        placeProblemService.update(params);
        return ReturnResult.success();
    }

    @GetMapping("list")
    @ApiOperation("查询")
    public ReturnResult queryPageList(PlaceProblemSearchRequest params){

        if(UserType.PLACE.equals(userUtil.getUser().getType())){
            String placeId = isolationPlaceMapper.getPlaceId(userUtil.getUser().getName());
            params.setPlaceId(placeId);
        }
        return ReturnResult.success(placeProblemService.pageList(params));
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public ReturnResult delete(@PathVariable("id") String id) {
        placeProblemService.removeById(id);
        return ReturnResult.success();
    }

    @GetMapping("get-details")
    @ApiOperation("查询场所详情")
    public ReturnResult getDetails(String id){
        return ReturnResult.success(placeProblemService.getDetails(id));
    }
}
